

import { _decorator, Button, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
import { ImageSprite } from "./ImageSprite";
import { Direction } from "./SlotEnum";
import { ResultModel } from "./Models/ResultModel";
import { SlotHorizontalView } from "./SlotHorizontalView";
import { testMockData } from "./Models/TestMockData";


@ccclass('GameManager')
export class GameManager extends Component {
    @property({ type: Node, tooltip: "游戏机的实体组件" })
    machine: Node = null;

    ///开始的按钮
    @property({ type: Button, tooltip: "spin按鈕" })
    spinBtn: Button = null;

    // 结果的模型
    private needFinish = false;
    private result: ResultModel = null;
    /** 滚轴数量 */
    private _colmnCount = 0;
    // 图片数量
    private _diceCount = 0;

    /// 计算属性获取有多少列
    get colmnCount(): number {
        if (this._colmnCount <= 0) {
            this._colmnCount = this.machine.getComponent(SlotHorizontalView).countColmn;
        }
        return this._colmnCount;
    }
    /// 每张图片有多少个纹理图片
    get diceCount(): number {
        if (this._diceCount <= 0) {
            this._diceCount = this.machine.getComponentInChildren(ImageSprite).diceCount;
        }

        return this._diceCount;
    }


    /// 生命周期函数开始
    protected start(): void {
        this.machine.getComponent(SlotHorizontalView).createMachine();
    }

    /// 生命周期函数每帧更新
    protected update(dt: number): void {
        if (this.needFinish && this.result != null) {
            console.log("开始了");
            this.informStop();
            this.result = null;
        }
    }

    click(): void {
        /// 是否正在滚动
        if (this.machine.getComponent(SlotHorizontalView).spinning == false) {
            this.needFinish = false;
            ///开始转动
            this.machine.getComponent(SlotHorizontalView).beginSpin();
            this.switchSpinBtn(true, "STOP!");
            this.requestResult();
        } else if (!this.needFinish) {
            this.needFinish = true;
            this.lock();
        }
    }

    /// 拿到后端结果 
    async requestResult() {
        this.result = null;
        this.result = await this.getAnswer();
        console.log("結果", this.result.reels);
    }

    /** 暂停按钮切换 */
    switchSpinBtn(isOpen: boolean, btnName: string): void {
        this.spinBtn.interactable = isOpen;
    }

    lock(): void {
        this.spinBtn.interactable = false;
    }

    /// 模拟mock数据
     getAnswer(): Promise<ResultModel> {
        return new Promise<ResultModel>(async (resolve) => {
            // 随机生成结果，实际会缓存fetch走http请求
            const randomIndex = Math.floor(Math.random() * testMockData.length);
            let answer: ResultModel = testMockData[randomIndex];
            resolve(answer);
        });
    }

    fetchData(): Promise<ResultModel[]> {
        // 模拟异步加载数据
        return new Promise((resolve) => {
            resolve(testMockData);
        });
    }

    informStop(): void {
        this.machine.getComponent(SlotHorizontalView).stopPin(this.result, (reslut) => {
            this.switchSpinBtn(true, '');
        });
    }   
}


